package com.jjh.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.jjh.common.controller.BaseController;
import com.jjh.common.pojo.InfoEnum;
import com.jjh.common.util.ConstatFinalUtil;
import com.jjh.users.pojo.AStudent;
import com.jjh.users.pojo.AStudentEnum;
import com.jjh.users.pojo.ATeacher;
import com.jjh.users.service.IUsersService;

/**
 * 老师的后台不需要登录
 * @author MyJJh
 *
 */
@Controller
@RequestMapping
public class NoLoginBackController extends BaseController
{

	@Autowired
	private IUsersService usersService ;
	
	/**
	 * 打开登录页面
	 * @return
	 */
	@RequestMapping("/login")
	public String login(HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("=======login=======");
		ATeacher teacher = (ATeacher) session.getAttribute("TeachersSess");
		if(teacher != null)
		{
			return "redirect:/back/teachers/mainTeacher";
		}
		return "/back/login" ;
	}
	
	/**
	 * 登录提交页面
	 * @return
	 */
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpSession session , HttpServletRequest request , Model model,
			String email , String password)
	{
		ConstatFinalUtil.LOGGER.info("=======loginSubmit=======");
		/* 先根据邮箱查询用户 */
		/* 先根据邮箱查询一下,是否重复 */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("email", email);
		ATeacher teachers = this.usersService.findOneTeacherService(condMap);
		if(teachers != null)
		{
			/* 用户已经找到 */
			if(this.encryptUtil.checkStr(password, teachers.getPassword()))
			{
				/* 密码正确 */
				if(teachers.getStatus() == AStudentEnum.STATUS_DISABLED.getStatus())
				{
					/* 状态正确 */
					/* 登陆成功 */
					/* 状态启用 */
					/* 放到session中*/
					session.setAttribute("lastLoginTime", teachers.getLastLoginTime());
					session.setAttribute("TeachersSess", teachers);
					
					/* 修改上次登录时间 */
					teachers.setLastLoginTime(new Date());
					/* 更新用户 */
					this.usersService.updateOneTeacherService(teachers);
					
					/* 登录成功 */
					return "redirect:/back/teachers/mainTeacher";
				}else
				{
					this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_STATUS_DISABLED.getCode() + "") ;
				}
			}else
			{
				this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_PASSWORD_INCORRECT.getCode() + "") ;
			}
		}
		model.addAttribute("info", info);
		return this.login(session);
	}
	
	/**
	 * 打开注册页面
	 * @return
	 */
	@RequestMapping("/register")
	public String register()
	{
		ConstatFinalUtil.LOGGER.info("=======register=======");
		return "/back/register" ;
	}
	
	
	/**
	 * 注册提交页面
	 * @return
	 */
	@RequestMapping("/registerSubmit")
	public String registerSubmit(ATeacher teachers ,HttpSession session,
			Model model)
	{
		ConstatFinalUtil.LOGGER.info("=======registerSubmit=======");
		
		/* 调用service的方法 */
		JSONObject resultJson = this.usersService.saveOneTeacherService(teachers);
		ConstatFinalUtil.LOGGER.info("返回结果:{}",resultJson.toJSONString());
		/* 存储到四大作用域中 */
		this.info = resultJson.getString("info") ; 
		model.addAttribute("info", info);
		/* 存储到四大作用中 (pageContext request session appliaction )*/
		model.addAttribute("teachers", teachers);
		return this.register();
	}

}
